//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.crash;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build.VERSION;
import android.os.Process;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

public class MyCrashHandler implements UncaughtExceptionHandler {
    private static final MyCrashHandler sMyCrashHandler = new MyCrashHandler();
    private UncaughtExceptionHandler mOldHandler;
    private Context mContext;
    private LinkedHashMap<String, Object> mCrashInfo = new LinkedHashMap();

    public MyCrashHandler() {
    }

    public static MyCrashHandler getInstance() {
        return sMyCrashHandler;
    }

    public void register(Context context) {
        if(context != null) {
            this.mContext = context;
            this.mOldHandler = Thread.getDefaultUncaughtExceptionHandler();
            if(this.mOldHandler != this) {
                this.initCrashInfo();
                Thread.setDefaultUncaughtExceptionHandler(this);
            }
        }

    }

    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            String[] e = new String[1];
            JSONObject crashJson = this.getCrashJson(ex, e);
            String newLogFile = this.initNewLogFile(this.mContext.getFilesDir().getParent() + "/UpdateLib/CrashLog");
            this.saveCrashInfo2DataDirEx(newLogFile, crashJson);
        } catch (Throwable var9) {
            ;
        } finally {
            if(this.mOldHandler != null) {
                this.mOldHandler.uncaughtException(thread, ex);
            }

        }

    }

    private void saveCrashInfo2DataDirEx(String newLogFile, JSONObject jsonCrashInfo) {
        if(jsonCrashInfo != null) {
            JSONArray jsonArray = this.getCrashJasonArray(newLogFile);
            jsonArray.put(jsonCrashInfo);
            FileUtils.writeBytesToFile(new File(newLogFile), jsonArray.toString().getBytes(), false);
        }

    }

    private JSONArray getCrashJasonArray(String logFile) {
        JSONArray jsonArray = null;
        File file = new File(logFile);
        if(file.exists()) {
            String lastCrashInfo = FileUtils.readFileToString(file);

            try {
                if(!TextUtils.isEmpty(lastCrashInfo)) {
                    jsonArray = new JSONArray(lastCrashInfo);
                }
            } catch (Exception var6) {
                FileUtils.deleteFile(file.getAbsolutePath());
            }
        }

        if(jsonArray == null) {
            jsonArray = new JSONArray();
        }

        return jsonArray;
    }

    private String initNewLogFile(String logDir) {
        File fileNew = new File(logDir, "MyCrashLog.newlog");
        return fileNew.getAbsolutePath();
    }

    private void initCrashInfo() {
        String packageName = this.mContext.getPackageName();
        this.mCrashInfo.put("AppPkgName", packageName);

        try {
            PackageInfo e = this.mContext.getPackageManager().getPackageInfo(packageName, 0);
            this.mCrashInfo.put("VersionCode", Integer.valueOf(e.versionCode));
            this.mCrashInfo.put("VersionName", e.versionName);
            this.mCrashInfo.put("Debug", Boolean.valueOf(0 != (e.applicationInfo.flags & 2)));
        } catch (Exception var4) {
            this.mCrashInfo.put("VersionCode", Integer.valueOf(-1));
            this.mCrashInfo.put("VersionName", "null");
            this.mCrashInfo.put("Debug", "Unkown");
        }

        this.mCrashInfo.put("ProcessName", this.getProcessName());

        try {
            this.mCrashInfo.put("m", this.getIMEI(this.mContext));
        } catch (Exception var3) {
            ;
        }

        this.mCrashInfo.put("ro.product.board", this.getString("ro.product.board", "unknown"));
        this.mCrashInfo.put("ro.bootloader", this.getString("ro.bootloader", "unknown"));
        this.mCrashInfo.put("ro.product.brand", this.getString("ro.product.brand", "unknown"));
        this.mCrashInfo.put("ro.product.cpu.abi", this.getString("ro.product.cpu.abi", "unknown"));
        this.mCrashInfo.put("ro.product.cpu.abi2", this.getString("ro.product.cpu.abi2", "unknown"));
        this.mCrashInfo.put("ro.product.device", this.getString("ro.product.device", "unknown"));
        this.mCrashInfo.put("ro.build.display.id", this.getString("ro.build.display.id", "unknown"));
        this.mCrashInfo.put("ro.build.fingerprint", this.getString("ro.build.fingerprint", "unknown"));
        this.mCrashInfo.put("ro.hardware", this.getString("ro.hardware", "unknown"));
        this.mCrashInfo.put("ro.build.host", this.getString("ro.build.host", "unknown"));
        this.mCrashInfo.put("ro.build.id", this.getString("ro.build.id", "unknown"));
        this.mCrashInfo.put("ro.product.manufacturer", this.getString("ro.product.manufacturer", "unknown"));
        this.mCrashInfo.put("ro.product.model", this.getString("ro.product.model", "unknown"));
        this.mCrashInfo.put("ro.product.name", this.getString("ro.product.name", "unknown"));
        this.mCrashInfo.put("gsm.version.baseband", this.getString("gsm.version.baseband", "unknown"));
        this.mCrashInfo.put("ro.build.tags", this.getString("ro.build.tags", "unknown"));
        this.mCrashInfo.put("ro.build.type", this.getString("ro.build.type", "unknown"));
        this.mCrashInfo.put("ro.build.user", this.getString("ro.build.user", "unknown"));
        this.mCrashInfo.put("ro.build.version.codename", this.getString("ro.build.version.codename", "unknown"));
        this.mCrashInfo.put("ro.build.version.incremental", this.getString("ro.build.version.incremental", "unknown"));
        this.mCrashInfo.put("ro.build.version.release", this.getString("ro.build.version.release", "unknown"));
        this.mCrashInfo.put("ro.build.version.sdk", this.getString("ro.build.version.sdk", "unknown"));
    }

    private Object getString(String key, String defValue) {
        Method method = ReflectUtils.getMethod("android.os.SystemProperties", "get", new Class[]{String.class, String.class});
        return ReflectUtils.invoke((Object)null, method, new Object[]{key, defValue});
    }

    private JSONObject getCrashJson(Throwable ex, String[] crashInfo) {
        JSONObject json = new JSONObject();
        StringBuilder builder = new StringBuilder();

        try {
            Iterator e = this.mCrashInfo.entrySet().iterator();

            while(e.hasNext()) {
                Entry entry = (Entry)e.next();
                String key = (String)entry.getKey();
                Object value = entry.getValue();
                builder.append(key).append(":").append(value).append("\n");
                json.put(key, value);
            }

            json.put("exception_message", ex.getLocalizedMessage());
            if(VERSION.SDK_INT >= 8) {
                json.put("_exception_detail", new String(Base64.encode(Log.getStackTraceString(ex).getBytes(), 2)));
            }
        } catch (JSONException var9) {
            var9.printStackTrace();
        }

        builder.append("----------------------------Exception StackTrace:\n");
        crashInfo[0] = builder.toString();
        return json;
    }

    private String getIMEI(Context mContext) {
        return DeviceUtils.getIMEI(mContext);
    }

    private String getProcessName() {
        ActivityManager am = (ActivityManager)this.mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List infos = am.getRunningAppProcesses();
        Iterator i$ = infos.iterator();

        RunningAppProcessInfo info;
        do {
            if(!i$.hasNext()) {
                return null;
            }

            info = (RunningAppProcessInfo)i$.next();
        } while(info.pid != Process.myPid());

        return info.processName;
    }
}
